package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *套餐管理接口实现类
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    private static final Logger log = LoggerFactory.getLogger(SetmealServiceImpl.class);
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        //1.保存套餐的基本信息到setmeal表中
        Setmeal setmeal = new Setmeal();
        //1.1属性拷贝
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        //插入套餐id，获取到套餐id
        Long setmealId = setmeal.getId();
        //2.保存套餐和菜品的关联关系到setmeal_dish表中--多条数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size()>0 ){
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
            //批量插入套餐和菜品的关联数据
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //1.分页查询参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        //2.分页查询套餐
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        //3.返回查询
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 套餐启售停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void updateWithDish(SetmealDTO setmealDTO) {
        //1.修改套餐表setmeal表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //2.删除套餐和菜品的关联数据--setmeal_dish表
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        //3.添加套餐和菜品的关联数据--setmeal_dish表
        if (setmealDTO.getSetmealDishes() != null && setmealDTO.getSetmealDishes().size()>0){
            setmealDTO.getSetmealDishes().forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
            setmealDishMapper.insertBatch(setmealDTO.getSetmealDishes());
        }
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        //1.查询套餐基本信息
        Setmeal setmeal = setmealMapper.getByIdWithDish(id);
        //2.查询套餐菜品信息
        List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealDishesBySetmealId(id);
        //3.封装数据
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        //非空校验
        if (ids == null || ids.size() == 0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_NOT_FOUND);
        }
        //启售中的套餐无法删除
        if (setmealMapper.countBySetmealId(ids) > 0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
       if (ids != null || ids.size() > 0){
           //1.批量删除套餐
           setmealMapper.deleteBatch(ids);
           //2.批量删除套餐和菜品的关联数据--setmeal_dish表
           setmealDishMapper.deleteBySetmealIds(ids);
       }
    }

    /**
     * 根据分类id查询套餐
     * @param setmeal
     * @return
     */
    @Override
    public List<SetmealVO> list(Setmeal setmeal) {
        List<SetmealVO> setmealList =setmealMapper.list(setmeal);
        return setmealList;
    }

    /**
     * 根据套餐id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemById(id);
    }
}
